/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package univalle.eisc.pais.ai.searcheslab.view;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;
import univalle.eisc.pais.ai.searcheslab.model.Penalty;
import univalle.eisc.pais.ai.searcheslab.model.Stage;
import univalle.eisc.pais.ai.searcheslab.model.VisibleObject;

/**
 *
 * @author victor
 */
public class SceneView extends JPanel {

    private static final long serialVersionUID = 1L;
    private static final Logger LOG = Logger.getLogger(SceneView.class.getName());
    private final int FRAME_WIDTH = 20;
    private int cell_w;
    private int cell_h;
    private Stage stage;

    public SceneView() {
        addListeners();
        repaint();
    }

    public Stage getStage() {
        return stage;
    }

    public void setStage(Stage stage) {
        this.stage = stage;
        calculateCellSize();
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D graphics = (Graphics2D) g;
        calculateCellSize();
        drawStage(graphics);
        drawPelanties(graphics);
        drawPlace1(graphics);
        drawPlace2(graphics);
        drawItems(graphics);
        drawItem2(graphics);
        drawRobot(graphics);

    }

    private void drawPelanties(Graphics2D g) {        

        for (Penalty penalty : stage.getPenalties()) {
            VisibleObject obj = transformObject(penalty);
            g.setColor(Color.RED);
            g.fillRect(obj.getX(), obj.getY(),
                    obj.getWidth(), obj.getHeight());
            g.setColor(Color.BLACK);
            g.setFont(new Font(Font.SERIF, Font.BOLD, 10));
            g.drawString("Penalización " + String.valueOf(penalty.getValue()), obj.getX(), obj.getY()+10);
        }

    }

    private void drawPlace1(Graphics2D g) {

        g.setColor(Color.BLACK);
        VisibleObject obj = transformObject(stage.getPlace1());


        g.fillRect(obj.getX(), obj.getY(),
                obj.getWidth(), obj.getHeight());
        
        g.setColor(Color.WHITE);
        g.setFont(new Font(Font.SERIF, Font.BOLD, 10));
        g.drawString("Lugar 1", obj.getX(), obj.getY() + 10);

    }

    private void drawPlace2(Graphics2D g) {

        g.setColor(Color.BLUE);
        VisibleObject obj = transformObject(stage.getPlace2());


        g.fillRect(obj.getX(), obj.getY(),
                obj.getWidth(), obj.getHeight());
        
        g.setColor(Color.WHITE);
        g.setFont(new Font(Font.SERIF, Font.BOLD, 10));
        g.drawString("Lugar 2", obj.getX(), obj.getY() + 10);

    }

    private void drawItems(Graphics2D g) {

        g.setColor(Color.GREEN);
        VisibleObject obj = transformObject(stage.getItem1());


        g.fillRect(obj.getX(), obj.getY(),
                obj.getWidth(), obj.getHeight());
        
        g.setColor(Color.WHITE);
        g.setFont(new Font(Font.SERIF, Font.BOLD, 10));
        g.drawString("Objeto 1", obj.getX(), obj.getY() + 10);

    }

    private void drawItem2(Graphics2D g) {

        g.setColor(Color.YELLOW);
        VisibleObject obj = transformObject(stage.getItem2());


        g.fillRect(obj.getX(), obj.getY(),
                obj.getWidth(), obj.getHeight());
        g.setColor(Color.BLACK);
        g.setFont(new Font(Font.SERIF, Font.BOLD, 10));
        g.drawString("Objeto 2", obj.getX(), obj.getY() + 10);
    }

    private void drawRobot(Graphics2D g) {

        g.setColor(Color.GRAY);
        VisibleObject obj = transformObject(stage.getRobot());


        g.fillRect(obj.getX(), obj.getY(),
                obj.getWidth(), obj.getHeight());
        
        g.setColor(Color.BLACK);
        g.setFont(new Font(Font.SERIF, Font.BOLD, 10));
        g.drawString("Robot", obj.getX(), obj.getY() + 10);
    }

    private void drawStage(Graphics2D g) {

        g.setColor(Color.RED);

        int dx, dy;
        int x_max = (int) getSize().getWidth() - FRAME_WIDTH;
        int y_max = (int) getSize().getHeight() - FRAME_WIDTH;
        int N = stage.getConfig().getN();
        for (int i = 0; i <= N; i++) {
            dy = FRAME_WIDTH + i * cell_h;
            dx = FRAME_WIDTH + i * cell_w;
            g.drawLine(FRAME_WIDTH, dy, x_max, dy);
            g.drawLine(dx, FRAME_WIDTH, dx, y_max);
        }
    }

    private VisibleObject transformObject(VisibleObject object) {
        int x, y;

        VisibleObject out = null;
        try {
            out = (VisibleObject) object.clone();
        } catch (CloneNotSupportedException ex) {
            Logger.getLogger(SceneView.class.getName()).log(Level.SEVERE, null, ex);
        }

        out.setWidth((int) (cell_w / 100.0 * object.getWidth()));
        out.setHeight((int) (cell_h / 100.0 * object.getHeight()));

        x = FRAME_WIDTH + object.getX() * cell_w;
        x += (int) (cell_w / 2.0 - out.getWidth() / 2.0);
        y = FRAME_WIDTH + object.getY() * cell_h;
        y += (int) (cell_h / 2.0 - out.getHeight() / 2.0);

        out.setX(x);
        out.setY(y);

        return out;
    }

    private void calculateCellSize() {
        Rectangle bounds = getBounds();
        cell_w = (bounds.width - FRAME_WIDTH * 2) / stage.getConfig().getN();
        cell_h = (bounds.height - FRAME_WIDTH * 2) / stage.getConfig().getN();
    }

    private void addListeners() {

        this.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent e) {
//                calculateCellSize();
                //System.out.println("component resized to: " + bounds.getWidth() + ", " + bounds.getHeight() + ", " + bounds.getX() + ", " + bounds.getY());
            }
        });
    }
}
